package com.wdh.kjs.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.R;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.model.NewsDetailsForAppModel;
import com.wdh.kjs.model.NewsDetailsForAppModelResult;
import com.wdh.kjs.util.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;


public class NewsDetailsForAppActivity extends BaseTopActivity {
    private HashMap<String, String> maps = new HashMap<String, String>();
    private TextView tv_news_detail_title;
    private TextView tv_news_detail_time;
    private TextView tv_news_detail_source;
    private TextView tv_news_detail_author;
    private WebView webView;

    private String platformNewsId;
    private NewsDetailsForAppModel newsDetailsForAppModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_details_for_app);
        platformNewsId = getIntent().getStringExtra("platformNewsId");
        initTopBar("新闻详情");
        initview();
        intweb();
    }

    private void intweb() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("platformNewsId", platformNewsId);

        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_NewsDetails_For_App, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Gson gson = new Gson();
                newsDetailsForAppModel = gson.fromJson(responseInfo.result, NewsDetailsForAppModel.class);
                NewsDetailsForAppModelResult ndfar=newsDetailsForAppModel.getOneNewsDetailsForAppModelResult();
                if (newsDetailsForAppModel.isSuccess()) {
                   // String time = DateUtilChange.timestamp2String(jsonModel.getMessagedetail().get(0).getCreatedTxStamp(), DateUtilChange.DEFAULT_SECOND_FORMAT);
                    tv_news_detail_title.setText(ndfar.getTitle());
                    String time=ndfar.getCreatedStamp();
                    tv_news_detail_time.setText(getTime(time));
                    tv_news_detail_source.setText(ndfar.getSource());
                    tv_news_detail_author.setText(ndfar.getAuthor());
                    String htmlstr= ndfar.getContent();
                   // String htmlstr="aaaa";
                    webView.loadDataWithBaseURL(null, htmlstr, "text/html", "utf-8", null);
                    webView.getSettings().setJavaScriptEnabled(true);
                    webView.setWebChromeClient(new WebChromeClient());
                }
                else{
                    tv_news_detail_title.setText("获取新闻详情失败,请返回上个页面");
                }
            }
            @Override
            public void onFailure(HttpException error, String msg) {
                ToastUtils.showShort(NewsDetailsForAppActivity.this, "网络异常！");
            }
        });
    }

    private void initview() {
        tv_news_detail_title=(TextView) findViewById(R.id.tv_news_detail_title);
        tv_news_detail_time=(TextView) findViewById(R.id.tv_news_detail_time);
        tv_news_detail_source=(TextView) findViewById(R.id.tv_news_detail_source);
        tv_news_detail_author=(TextView) findViewById(R.id.tv_news_detail_author);
        webView=(WebView)findViewById(R.id.webView_news_detail);
    }
    private String getTime(String date_str){
        String format="yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(date_str)));
    }
}
